<?php
////JS check en mask for price, salePrice, shopCost
$shopAssetsPath = Yii::getPathOfAlias('Shop.assets');
if ($shopAssetsPath) {
    $shopAssetsUrl = Yii::app()->assetManager->publish($shopAssetsPath);
    cs()->registerScriptFile($shopAssetsUrl . '/autoNumeric.js');
    cs()->registerScriptFile($shopAssetsUrl . '/jquery.numeric.js');
}
?>
<div class="control-group">
    <?php echo $form->labelEx($model, 'sku', array('class' => 'control-label', 'label' => 'SKU')); ?>
    <div class="controls">
        <?php echo $form->textField($model, 'sku'); ?>
        <?php echo $form->error($model, 'sku'); ?>
    </div>
</div>
<div class="control-group">
    <?php echo $form->labelEx($model, 'upc', array('class' => 'control-label', 'label' => 'UPC')); ?>
    <div class="controls">
        <?php echo $form->textField($model, 'upc'); ?>
        <?php echo $form->error($model, 'upc'); ?>
    </div>
</div>
<div class="control-group">
    <?php echo $form->labelEx($model, 'item_numer', array('class' => 'control-label', 'label' => 'Manufacture Part No')); ?>
    <div class="controls">
        <?php echo $form->textField($model, 'item_numer'); ?>
        <?php echo $form->error($model, 'item_numer'); ?>
    </div>
</div>

<?php if (is_array($prices) && count($prices)): ?>
    <div class="control-group buttons">
        <label class="control-label">Prices</label>

        <div class="controls">
            <table class="prices-form">
                <tr>
                    <th>Price level</th>
                    <th>Normal price</th>
                    <th>Sale price</th>
                </tr>
                <?php foreach ($prices as $i => $price): ?>
                    <tr>
                        <td><?php
                            //store price_level name to id, now loaded
                            echo CHtml::encode($price->id); ?></td>
                        <td>
                            <?php echo $form->textField($price, "[$i]price", array('class' => 'price')); ?>
                            <!--                    --><?php //echo $form->error($price, "[$i]price"); ?>
                        </td>
                        <td>
                            <?php echo $form->textField($price, "[$i]sale_price", array('class' => 'sale-price')); ?>
                            <!--                    --><?php //echo $form->error($price, "[$i]sale_price"); ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </table>
        </div>
    </div>
<?php endif; ?>

<div class="control-group">
    <label class="control-label">Shipping Cost</label>

    <div class="controls">
        <?php $model->charge_shipping_option = SubProduct::SHIP_CHARGE_OPTION_FIXED;
        echo $form->dropdownList($model, 'charge_shipping_option', $model->getShipChargeOptions()); ?>
        <?php echo $form->textField($model, 'shipping_cost', array('style' => 'width: 120px;', 'maxlength' => 12)); ?>
    </div>
</div>

<div class="control-group">
    <label class="control-label">Stock Total : </label>

    <div class="controls">
        <?php $model->stock_total = CPropertyValue::ensureInteger($model->stock_total);
        echo $form->textField($model, 'stock_total', array('style' => 'width: 120px;', 'maxlength' => 12, 'value'=>'99999999')); ?>
    </div>
</div>

<?php
$attrs = $this->getAttributeGroupsForNewSubproduct();
if (count($attrs)) :
    echo '<legend>Attributes</legend>';
    foreach ($attrs as $key => $attr):
        ?>
        <div class="control-group">
            <label class="control-label"><?php echo $attr['name']; ?></label>

            <div class="controls">
                <?php
                if ($attr['type'] == AttributeGroup::TYPE_MULTIPLE)
                    echo CHtml::dropDownList('attribute_' . $key . '[]', '', $attr['data'], array('multiple' => 'multiple'));
                else
                    echo CHtml::dropDownList('attribute_' . $key, '', $attr['data'], array('prompt' => '---Select---'));
                ?>
            </div>
        </div>
    <?php
    endforeach;
endif;?>

<script type="text/javascript">
    //Validate price and sale-price
    $('.prices-form input:text').change(function () {
        value = $(this).val().replace(',', '').replace('-', '');
        if ($.isNumeric(value)) {
            value = parseFloat(value).toFixed(2);
            var parts = value.split('.');
            var part1 = parts[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
            var part2 = parts[1];
            $(this).val(part1 + '.' + part2);
        } else {
            $(this).val('0.00');
        }
    });
    $('.prices-form input:text').trigger('change');
    function validateSubmit() {
        var errorMess = '';
        var price = 0, salePrice = 0;
        $('.prices-form input:text').each(function () {
            if ($(this).hasClass('price')) {
                price = $(this).val().replace(",", "");
                price = parseFloat(price).toFixed(2);
                salePrice = parseFloat($(this).closest('tr').find('.sale-price').val().replace(",", "")).toFixed(2);
                if (salePrice > price)
                    errorMess = 'Sale Price must be less than or equal Price';
            }
        });
        return errorMess;
    }
    //Validate ship cost and ship-type
    $('#SubProduct_stock_total').autoNumeric('init', {aSep: '', aPad: false, wEmpty: 'zero'});
    $('#SubProduct_stock_total').numeric({ decimal: false});
    $('#SubProduct_shipping_cost').autoNumeric('init', {vMin: '-999999999.99', wEmpty: 'zero'});
    $('#SubProduct_charge_shipping_option').data('shipCost', $('#SubProduct_shipping_cost').val());
    $('#SubProduct_charge_shipping_option').data('shipType', $('#SubProduct_charge_shipping_option').val());
    $('#SubProduct_charge_shipping_option').change(function () {
        var shipCost = $(this).data('shipCost');
        var shipType = $(this).data('shipType');
        if ($(this).val() == 'surcharge') {
            $('#SubProduct_shipping_cost').val('0.00').prop('disabled', false);
            $('#SubProduct_shipping_cost').autoNumeric('update', {vMin: '-999999999.99'});
        } else if ($(this).val() == 'fixed') {
            $('#SubProduct_shipping_cost').val('0.00').prop('disabled', false);
            $('#SubProduct_shipping_cost').autoNumeric('update', {vMin: '0.00'});
        } else {
            $('#SubProduct_shipping_cost').val('0.00').prop('disabled', true);
        }
        if ($(this).val() == shipType) {
            $('#SubProduct_shipping_cost').val(shipCost);
        }
    })
    $('#SubProduct_charge_shipping_option').trigger('change');
</script>